import 'package:flutter/material.dart';
import '../../share/my_color.dart';
import '../../share/size_fill.dart';

class ShowSegmenWidget extends StatefulWidget {
  double? itemHeight;
  double? itemWidth;
  final Color backGroundColor;
  double? radius;
  final List<String> items;
  int currentIndex; //当前选中的item

  final Color itemNorBgColor;
  final Color itemSelectBgColor;

  final Color itemNorTextColor;
  final Color itemSelectTextColor;
  final Color itemBorderColor;

  final Function(int index)? function;
  double? itemFont;

  ShowSegmenWidget({
    this.itemHeight,
    this.itemWidth,
    this.backGroundColor = MyColors.color_247_247_247,
    this.radius,
    this.items = const ["最新", "热门"],
    this.currentIndex = 0,
    this.itemNorBgColor = MyColors.color_247_247_247,
    this.itemSelectBgColor = MyColors.color_255_255_255,
    this.itemFont,
    this.itemNorTextColor = MyColors.color_102_102_102,
    this.itemSelectTextColor = MyColors.color_34_34_34,
    this.itemBorderColor = MyColors.color_221_221_221,
    this.function,
  });

  @override
  _ShowSegmenWidgetState createState() => _ShowSegmenWidgetState();
}

class _ShowSegmenWidgetState extends State<ShowSegmenWidget> {
  @override
  Widget build(BuildContext context) {
    widget.itemHeight ??= SizeFill.setRx(30);
    widget.itemWidth ??= SizeFill.setRx(48);
    widget.radius ??= SizeFill.setRx(15);
    widget.itemFont ??= SizeFill.setRx(12);
    return Container(
      height: widget.itemHeight,
      padding: EdgeInsets.zero,
      decoration: BoxDecoration(
        color: widget.backGroundColor,
        borderRadius: BorderRadius.circular(widget.radius!),
      ),
      child: Row(

        children: List.generate(widget.items.length, (index) {
          bool select = widget.currentIndex == index;
          return ButtonTheme(
            height: widget.itemHeight!,
            minWidth: widget.itemWidth!,
            child: FlatButton(
              shape: RoundedRectangleBorder(
                side: BorderSide(color:select? widget.itemBorderColor:widget.backGroundColor, width: 0.5),
                borderRadius: BorderRadius.circular(widget.radius!),
              ),
              onPressed: () {
                if(widget.function != null){
                  widget.function!(index);
                }
                setState(() {
                  widget.currentIndex = index;
                });
              },
              color:select? widget.itemSelectBgColor:widget.itemNorBgColor,
                child: Text(
                  widget.items[index],
                  style: TextStyle(
                    fontSize: widget.itemFont,
                    fontWeight:select ? FontWeight.w500 : FontWeight.w400,
                    color: select ? widget.itemSelectTextColor : widget.itemNorTextColor,
                  ),
                ),
            ),
          );
        }),
      ),
    );
  }
}
